package cn.com.ghy.android.widget.salemag;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;
import cn.com.ghy.android.bean.YGOrderBaseInfoBean;
import cn.com.ghy.android.bean.YGOrderDetailInfoBean;
import cn.com.ghy.android.widget.R;

public class SaleOrderView extends TabActivity {

	private TabHost tabHost;

	private TextView txtVkorg;
	private TextView txtVtweg;
	private TextView txtAuart;
	private TextView txtVkbur;
	private TextView txtVkgrp;
	private TextView txtKunag;
	private TextView txtKunnr;
	private TextView txtStrasno;
	private TextView txtPernr;
	private TextView txtBstkd;
	private TextView txtBstdk;
	private TextView txtVdatu;
	private TextView txtRemark;

	private TextView labTxtOrderTotalPrice;
	private ListView listOrderDetailList;

	private Intent intent;

	private Map<String, Object> map = new HashMap<String, Object>();
	private List<YGOrderBaseInfoBean> yGOrderBaseInfoBeanList = new ArrayList<YGOrderBaseInfoBean>();
	private List<YGOrderDetailInfoBean> yGOrderDetailInfoBeanList = new ArrayList<YGOrderDetailInfoBean>();
	private YGOrderBaseInfoBean yGOrderBaseInfoBean;

	private OrderDetailInfoAdapter orderDetailInfoAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sale_order_view);
		getSession();
		tabHost = this.getTabHost();
		TabWidget tabWidget = tabHost.getTabWidget();

		tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("基本信息")
				.setContent(R.id.layOrderBaseInfo));
		tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("订单明细")
				.setContent(R.id.layOrderDetail));
		for (int i = 0; i < tabWidget.getChildCount(); i++) {
			tabWidget.getChildAt(i).getLayoutParams().height = this.getWindowManager()
					.getDefaultDisplay().getHeight() / 16;
		}
		findView();
		init();
	}

	@SuppressWarnings("unchecked")
	public void getSession() {
		intent = this.getIntent();
		map = (Map<String, Object>) intent.getSerializableExtra("order");
		yGOrderBaseInfoBeanList = (List<YGOrderBaseInfoBean>) map.get("orderBase");
		yGOrderBaseInfoBean = yGOrderBaseInfoBeanList.get(0);
		yGOrderDetailInfoBeanList = (List<YGOrderDetailInfoBean>) map.get("orderDetail");
	}

	private void findView() {

		txtVkorg = (TextView) findViewById(R.id.txtVkorg);
		txtVtweg = (TextView) findViewById(R.id.txtVtweg);
		txtAuart = (TextView) findViewById(R.id.txtAuart);
		txtKunag = (TextView) findViewById(R.id.txtKunag);
		txtKunnr = (TextView) findViewById(R.id.txtKunnr);
		txtStrasno = (TextView) findViewById(R.id.txtStrasno);
		txtPernr = (TextView) findViewById(R.id.txtPernr);
		txtBstkd = (TextView) findViewById(R.id.txtBstkd);
		txtRemark = (TextView) findViewById(R.id.txtRemark);
		txtBstdk = (TextView) findViewById(R.id.txtBstdk);
		txtVdatu = (TextView) findViewById(R.id.txtVdatu);
		txtVkbur = (TextView) findViewById(R.id.txtVkbur);
		txtVkgrp = (TextView) findViewById(R.id.txtVkgrp);

		labTxtOrderTotalPrice = (TextView) findViewById(R.id.labTxtOrderTotalPrice);
		listOrderDetailList = (ListView) findViewById(R.id.listOrderDetailList);
	}

	private void init() {

		txtVkorg.setText(yGOrderBaseInfoBean.getVkorg() == null ? "" : yGOrderBaseInfoBean
				.getVkorg());
		txtVtweg.setText(yGOrderBaseInfoBean.getVtweg() == null ? "" : yGOrderBaseInfoBean
				.getVtweg());
		txtAuart.setText(yGOrderBaseInfoBean.getAuart() == null ? "" : yGOrderBaseInfoBean
				.getAuart());
		txtVkbur.setText(yGOrderBaseInfoBean.getVkbur());
		txtVkgrp.setText(yGOrderBaseInfoBean.getVkgrp());
		txtKunag.setText(yGOrderBaseInfoBean.getKunag() == null ? "" : yGOrderBaseInfoBean
				.getKunag());
		txtKunnr.setText(yGOrderBaseInfoBean.getKunnr() == null ? "" : yGOrderBaseInfoBean
				.getKunnr());
		txtStrasno.setText(yGOrderBaseInfoBean.getStrasno() == null ? "" : yGOrderBaseInfoBean
				.getStrasno());
		txtPernr.setText(yGOrderBaseInfoBean.getPernr() == null ? "" : yGOrderBaseInfoBean
				.getPernr());
		txtBstkd.setText(yGOrderBaseInfoBean.getBstkd() == null ? "" : yGOrderBaseInfoBean
				.getBstkd());
		txtBstdk.setText(yGOrderBaseInfoBean.getBstdk() == null ? "" : yGOrderBaseInfoBean
				.getBstdk());
		txtVdatu.setText(yGOrderBaseInfoBean.getVdatu() == null ? "" : yGOrderBaseInfoBean
				.getVdatu());
		txtRemark.setText(yGOrderBaseInfoBean.getRemark() == null ? "" : yGOrderBaseInfoBean
				.getRemark());

		labTxtOrderTotalPrice.setText("0.00");

		orderDetailInfoAdapter = new OrderDetailInfoAdapter(this);
		listOrderDetailList.setAdapter(orderDetailInfoAdapter);
		listOrderDetailList.setOnItemClickListener(new OrderDataLisEvent());

		for (int i = 0; i < yGOrderDetailInfoBeanList.size(); i++) {
			orderDetailInfoAdapter.addOrder(yGOrderDetailInfoBeanList.get(i));
		}
	}

	private class OrderDataLisEvent implements AdapterView.OnItemClickListener {

		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			OrderDetailInfoAdapter orderInfoAdapter = (OrderDetailInfoAdapter) (parent.getAdapter());
			showAlertDialog(SaleOrderView.this, position, orderInfoAdapter.getItem(position));

		}
	}

	private class OrderDetailInfoAdapter extends BaseAdapter {

		private Context context;
		private List<YGOrderDetailInfoBean> list = new ArrayList<YGOrderDetailInfoBean>();

		public OrderDetailInfoAdapter(Context context) {
			this.context = context;
		}

		public int getCount() {
			return list.size();
		}

		public YGOrderDetailInfoBean getItem(int position) {
			return list.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			String inflater = Context.LAYOUT_INFLATER_SERVICE;
			LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(inflater);
			LinearLayout linearLayout = null;
			YGOrderDetailInfoBean bean = list.get(position);
			linearLayout = (LinearLayout) layoutInflater.inflate( //
					R.layout.sale_order_detail_lay, null);
			TextView textView = (TextView) linearLayout.findViewById(R.id.labBismt);
			textView.setText(bean.getBismt());

			textView = (TextView) linearLayout.findViewById(R.id.labUnit);
			textView.setText(bean.getUnit());

			textView = (TextView) linearLayout.findViewById(R.id.labMatnr);
			textView.setText(bean.getMatnr());

			textView = (TextView) linearLayout.findViewById(R.id.labOrdernum);
			textView.setText(bean.getOrdernum());

			textView = (TextView) linearLayout.findViewById(R.id.labOrderprice);
			textView.setText(bean.getOrderprice() + "元");

			textView = (TextView) linearLayout.findViewById(R.id.labDnm);
			textView.setText(bean.getDnm());

			textView = (TextView) linearLayout.findViewById(R.id.labIsfin);

			textView = ((TextView) linearLayout.findViewById(R.id.labTotalprice));
			textView.setText(bean.getTotalprice() + "元");
			return linearLayout;
		}

		public void addOrder(YGOrderDetailInfoBean orderDetail) {
			for (int i = 0; i < list.size(); i++) {
				if (orderDetail.getBismt().equals(list.get(i).getBismt())) {
					Toast.makeText(SaleOrderView.this, "SKU:" + orderDetail.getBismt() + "不能重复!",
							Toast.LENGTH_SHORT).show();
					return;
				}
			}
			list.add(orderDetail);
			totalPriceCount(list);
			notifyDataSetChanged();
		}

		private void totalPriceCount(List<YGOrderDetailInfoBean> list) {
			double totalPrice = 0;
			for (YGOrderDetailInfoBean orderDetail : list) {
				totalPrice += Double.parseDouble(orderDetail.getTotalprice());
			}
			java.text.DecimalFormat df = new java.text.DecimalFormat("######0.00");
			labTxtOrderTotalPrice.setText(df.format(totalPrice));
		}
	}

	private void showAlertDialog(final Context context, final int position, //
			final YGOrderDetailInfoBean orderDetail) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle("提示信息:");
		builder.setMessage("SKU编号：" + orderDetail.getBismt());
		builder.setPositiveButton("查看", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				intent = new Intent(SaleOrderView.this, SaleOrderDetailInfoView.class);
				Bundle bundle = new Bundle();
				bundle.putSerializable("yGOrderDetailInfoBean", orderDetail);
				intent.putExtras(bundle);
				SaleOrderView.this.startActivity(intent);
			}
		});
		builder.show();
	}
}
